package lesson4.api;

import java.util.concurrent.TimeUnit;

import lesson4.api.helpers.LocatorHelper;
import lesson4.api.helpers.NavigationHelper;
import lesson4.api.helpers.PageHelper;
import lesson4.api.helpers.WebElementHelper;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ApplicationManager
{
	private static Logger log = LoggerFactory.getLogger(ApplicationManager.class);
	// Site URL
	public final static String SITE_URL = "https://redmine.cupidplc.com";
	// Browser name
	public final static String BROWSER_NAME = "ch";
	// Test page
	public String testPage;
	// Web drivers
	public WebDriver driver;
	// Page helpers
	private PageHelper pageHelper;
	private NavigationHelper navigationHelper;
	private WebElementHelper webElementHelper;
	private LocatorHelper locatorHelper;

	public ApplicationManager()
	{

	}

	public void init()
	{
		log.debug("Start browser: " + BROWSER_NAME);
		setDriver(BROWSER_NAME);
		log.info("Maximize the browser window");
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
	}

	private void setDriver(String p_browserName)
	{
		if (p_browserName.equals("ff"))
		{
			driver = new FirefoxDriver();
		}
		else if (p_browserName.equals("ch"))
		{
			System.setProperty("webdriver.chrome.driver", "d:/projects/aft-exercises/libs/drivers/chromedriver.exe");
			driver = new ChromeDriver();
		}
		else if (p_browserName.equals("ie"))
		{
			System.setProperty("webdriver.ie.driver", "d:/projects/aft-exercises/libs/drivers/IEDriverServer.exe");
			driver = new InternetExplorerDriver();
		}

	}

	public void stop()
	{
		// Stop a driver
		driver.quit();
	}

	public PageHelper page()
	{
		if (pageHelper == null)
		{
			pageHelper = new PageHelper(this);
		}
		return pageHelper;
	}

	public NavigationHelper navigation()
	{
		if (navigationHelper == null)
		{
			navigationHelper = new NavigationHelper(this);
		}
		return navigationHelper;
	}

	public WebElementHelper webElement()
	{
		if (webElementHelper == null)
		{
			webElementHelper = new WebElementHelper(this);
		}
		return webElementHelper;
	}

	public LocatorHelper locator()
	{
		if (locatorHelper == null)
		{
			locatorHelper = new LocatorHelper(this);
		}
		return locatorHelper;
	}

}
